package com.tang.thepath;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.tang.thepath.model.RequestModel;
import com.tang.thepath.service.BizService;
import com.tang.thepath.service.HttpSubscriber;
import com.tang.thepath.view.ImageUploader;

import java.util.ArrayList;

public class VisitActivity extends BaseActivity {

    private EditText editCustomName;
    private EditText editHowmany;
    private EditText editContact;
    private EditText editCompany;
    private EditText editAtitude;
    private EditText editTreatplan;
    private ImageUploader imageUploader;
    private Button btnSave;


    @Override
    public ImageUploader getImageUploader() {
        return imageUploader;
    }
    /**
     * EditText竖直方向是否可以滚动
     * @param editText  需要判断的EditText
     * @return  true：可以滚动   false：不可以滚动
     */
    private boolean canVerticalScroll(EditText editText) {
        //滚动的距离
        int scrollY = editText.getScrollY();
        //控件内容的总高度
        int scrollRange = editText.getLayout().getHeight();
        //控件实际显示的高度
        int scrollExtent = editText.getHeight() - editText.getCompoundPaddingTop() -editText.getCompoundPaddingBottom();
        //控件内容总高度与实际显示高度的差值
        int scrollDifference = scrollRange - scrollExtent;

        if(scrollDifference == 0) {
            return false;
        }

        return (scrollY > 0) || (scrollY < scrollDifference - 1);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_visit);

        //editCustomName = (EditText) findViewById(R.id.edit_contact);
        editHowmany = (EditText) findViewById(R.id.edit_howmany);
        editContact = (EditText) findViewById(R.id.edit_contact);
        editCompany = (EditText) findViewById(R.id.edit_company);
        //editAtitude = (EditText) findViewById(R.id.edit_atitude);
        editTreatplan = (EditText) findViewById(R.id.edit_treatplan);
        imageUploader = (ImageUploader) findViewById(R.id.img_uploader);
        btnSave = (Button) findViewById(R.id.btn_save);

        editTreatplan.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //触摸的是EditText并且当前EditText可以滚动则将事件交给EditText处理；否则将事件交由其父类处理
                if ((v.getId() == R.id.edit_treatplan && canVerticalScroll(editTreatplan))) {
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                    }
                }
                return false;
            }
        });

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Const.NETWORK_MOD == 0){
                    toast("请检查您的网络设置");
                    return;
                }
                //String customName = editCustomName.getText().toString();
                String howmany = editHowmany.getText().toString();
                String contact = editContact.getText().toString();
                //String attitude = editAtitude.getText().toString();
                String treatplan = editTreatplan.getText().toString();
                String company = editCompany.getText().toString();

                if(contact.isEmpty()){
                    toast("联系人不能为空");
                    return;
                }
                if(company.isEmpty()){
                    toast("来访单位不能为空");
                    return;
                }
                if(howmany.isEmpty()){
                    toast("来访人数不能为空");
                    return;
                }
                if(treatplan.isEmpty()){
                    toast("接待计划不能为空");
                    return;
                }


                final RequestModel.StoreVisitRequest request = new RequestModel.StoreVisitRequest();
                request.contact = contact;
                request.howmany = Integer.valueOf(howmany);
                request.treatplan = treatplan;
                request.company = company;
                updateVisitInfo(request);

                /*if(imageUploader.imageCount() > 0){
                    showLoading("正在上传图片");
                    imageUploader.uploadImages().subscribe(new HttpSubscriber<RequestModel.UploadResponse>() {
                        @Override
                        protected void onSuccess(RequestModel.UploadResponse uploadResponse) {
                            dismisLoading();
                            request.images = uploadResponse.pathList;
                            updateVisitInfo(request);
                        }
                    });
                }else{
                    request.images = new ArrayList<String>();
                    updateVisitInfo(request);
                }*/
            }
        });
    }

    protected void updateVisitInfo(final RequestModel.StoreVisitRequest request){
        showLoading("数据提交中");
        BizService.storeVisit(request).subscribe(new HttpSubscriber<RequestModel.StoreVisitResponse>() {
            @Override
            protected void onSuccess(RequestModel.StoreVisitResponse storeVisitResponse) {
                String message = "存储成功:"+storeVisitResponse.id;
                Log.i(getTag(),message);
                toast(message);
                dismisLoading();
                finish();
            }
        });
    }
}
